package com.open.demo

import android.app.Application
import android.util.Log
import com.open.demo.matrix.DynamicConfigImplDemo
import com.open.demo.matrix.TestPluginListener
import com.open.demo.utils.Constats
import com.tencent.matrix.Matrix
import com.tencent.matrix.iocanary.IOCanaryPlugin
import com.tencent.matrix.iocanary.config.IOConfig
import com.tencent.matrix.resource.ResourcePlugin
import com.tencent.matrix.resource.config.ResourceConfig
import com.tencent.matrix.trace.TracePlugin
import com.tencent.matrix.trace.config.TraceConfig

class App : Application() {

    override fun onCreate() {
        super.onCreate()
//        initMatrix()
    }

     private fun initMatrix() {
         Log.d(Constats.TAG, "initMatrix start");
         // dynamic config
         val dynamicConfig = DynamicConfigImplDemo();
         val matrixEnable = dynamicConfig.isMatrixEnable();
         val fpsEnable = dynamicConfig.isFPSEnable();
         val traceEnable = dynamicConfig.isTraceEnable();

         // build matrix
         val builder = Matrix.Builder(App@this);
         // add general pluginListener
         builder.pluginListener(TestPluginListener(applicationContext));

         // add Trace Canary plugin.
         // 监控界面流畅性、启动耗时、页面切换耗时、慢函数及卡顿等问题
         val traceConfig = TraceConfig.Builder()
                 .dynamicConfig(dynamicConfig)
                 .enableFPS(fpsEnable)
                 .enableEvilMethodTrace(traceEnable)
                 .enableAnrTrace(traceEnable)
                 .enableStartup(traceEnable)
                 .isDebug(true)
                 .isDevEnv(false)
                 .build();
         val tracePlugin = TracePlugin(traceConfig);
         builder.plugin(tracePlugin);


         if (matrixEnable) {
             // add IO Canary plugin.
             // 检测文件 IO 问题，包括文件 IO 监控和 Closeable Leak 监控
             val ioCanaryPlugin = IOCanaryPlugin(
                 IOConfig.Builder()
                     .dynamicConfig(dynamicConfig)
                     .build());
             builder.plugin(ioCanaryPlugin);

             // add resource Canary plugin.
             // 基于 WeakReference 的特性和 Square Haha 库开发的 Activity 泄漏和 Bitmap 重复创建检测工具
              builder.plugin(ResourcePlugin(
                  ResourceConfig.Builder()
                      .dynamicConfig(dynamicConfig)
                      .setDetectDebuger(true) // only set true when in sample, not in your app
                      .build())
              );
              ResourcePlugin.activityLeakFixer(this);
         }

         //init matrix
         Matrix.init(builder.build());
         // start only startup tracer, close other tracer.
         tracePlugin.start();
         Log.d(Constats.TAG, "initMatrix end");
     }

}